package isssr.boundary.src;


import isssr.control.src.OrganizationalGoalFinderControl;
import isssr.entity.src.GoalStatus;
import isssr.entity.src.OrganizationalGoal;
import isssr.entity.src.OrganizationalGoalDTO;
import isssr.entity.src.Role;
import isssr.entity.src.User;
import isssr.entity.src.UserDTO;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.model.SelectItem;

import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortOrder;


/**
 * @author Ludovico 
 *
 */
@ManagedBean(name="organizationalGoalFinderBoundary")
@ViewScoped
public class OrganizationalGoalFinderBoundary extends LazyDataModel<OrganizationalGoalDTO> implements Serializable {

	private static final long 		serialVersionUID = -6646850497040376033L;
	private final static String[] 	states;
	private SelectItem[]			stateOptions;
	
	
	@ManagedProperty(value="#{organizationalGoalFinderControl}")
	private OrganizationalGoalFinderControl ctrl;
	
	
	static{	
		GoalStatus[] possible_states =  GoalStatus.values();
		states = new String[possible_states.length];
		for(int i=0; i<possible_states.length; i++)
			states[i] = possible_states[i].name();	
	}
	
	
	public OrganizationalGoalFinderBoundary(){
		this.stateOptions = this.createFilterOptions(states);
		 
	}

	
	public void setCtrl(OrganizationalGoalFinderControl ctrl) {
		this.ctrl = ctrl;
	}

	
	public SelectItem[] getStateOptions() {
		return stateOptions;
	}

	
	@Override
	public void setRowIndex(int rowIndex) {
	    /*
	     * The following is in ancestor (LazyDataModel):
	     * this.rowIndex = rowIndex == -1 ? rowIndex : (rowIndex % pageSize);
	     */
	    if (rowIndex == -1 || getPageSize() == 0) {
	        super.setRowIndex(-1);
	    }
	    else
	        super.setRowIndex(rowIndex % getPageSize());
	}
	
	
	@Override
	public List<OrganizationalGoalDTO> load(int first, int pageSize, String sortField,
			SortOrder sortOrder, Map<String, String> filters) {
		
		System.out.println("first:\t"+first);
		System.out.println("page size:\t"+pageSize);
		if(sortField != null)
			System.out.println("sort field:\t"+sortField);
		if(sortOrder != null)
			System.out.println("sort order:\t"+sortOrder.name());
		
		System.out.println("filters:............");
		for(Map.Entry<String, String> entry : filters.entrySet())
		{
			System.out.println("key:\t"+entry.getKey());
			System.out.println("value:\t"+entry.getValue());

		}
		
		List<OrganizationalGoalDTO> resultList = new ArrayList<OrganizationalGoalDTO>();
		int resultNumber = 0; 
		resultNumber = this.ctrl.load(first, pageSize, sortField, sortOrder, filters, resultList);
		
		System.out.println("result number:\t"+resultNumber);
		this.setRowCount(resultNumber);
		System.out.println("row count:\t"+this.getPageSize());
		System.out.println("page Size:\t"+this.getPageSize());
		System.out.println("row index:\t"+this.getRowIndex());
		return resultList; 
	}
	
	
	private SelectItem[] createFilterOptions(String[] data)  {  
		SelectItem[] options = new SelectItem[data.length + 1];  

		options[0] = new SelectItem("", "Select");  
		for(int i = 0; i < data.length; i++) {  
			options[i + 1] = new SelectItem(data[i], data[i]);  
		}  

		return options;  
	}
	
	
}
